package com.lmb.service.impl;

import com.lmb.bean.AdministratorLogin;
import com.lmb.bean.UserLogin;
import com.lmb.mapper.AdministratorLoginDao;
import com.lmb.service.AdministratorLoginService;
import com.lmb.util.Page;
import com.lmb.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AdministratorLoginServiceImpl implements AdministratorLoginService {
    public static void main(String[] args) {
    }
    @Autowired
    AdministratorLoginDao AdministratorLoginDao;
    @Override
    public AdministratorLogin add(AdministratorLogin AdministratorLogin) {
        AdministratorLoginDao.add(AdministratorLogin);
        AdministratorLogin result= AdministratorLoginDao.one(AdministratorLogin.getAd_id());
        return result;
    }

    @Override
    public int del(Integer id) {
        return AdministratorLoginDao.del(id);
    }

    @Override
    public AdministratorLogin edit(AdministratorLogin AdministratorLogin) {
        AdministratorLoginDao.edit(AdministratorLogin);
        AdministratorLogin result= AdministratorLoginDao.one(AdministratorLogin.getAd_id());
        return result;
    }

    @Override
    public AdministratorLogin one(Integer id) {
        return AdministratorLoginDao.one(id);
    }

    @Override
    public List<AdministratorLogin> all(AdministratorLogin AdministratorLogin) {
        return AdministratorLoginDao.all(AdministratorLogin);
    }

    @Override
    public void page(Page page, AdministratorLogin AdministratorLogin) {
        int count= AdministratorLoginDao.pageCount(AdministratorLogin);
        Object data= AdministratorLoginDao.page(page, AdministratorLogin);
        page.setCount(count);
        page.setData(data);
    }

    @Override
    public AdministratorLogin reset(String old_password, String new_password, Integer ad_id) {
        if (ad_id==null){
            throw new RuntimeException("用户未登录");
        }
        if(!PasswordUtil.checkPassword(old_password,AdministratorLoginDao.one(ad_id).getAd_password())){
            throw new RuntimeException("原密码错误");
        }
        AdministratorLogin user=AdministratorLoginDao.one(ad_id);
        user.setAd_password(PasswordUtil.getEncryption(new_password));
        AdministratorLoginDao.edit(user);
        return user;
    }
}
